﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;
using System.Web.UI.HtmlControls;

namespace BugTracker.Buttons
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [ToolboxData("<{0}:BTButtons runat=server></{0}:BTButtons>")]
    public class BTButtons : Button
    {

        public enum ButtonMode
        {
            Add,
            Save,
            Cancel,
            ChangePassword,
            Refresh,
            Filter,
            None,
            Return,
            Delete,
            Preview,
            Report,
            Excel,
            Login,
            Default
        }

        public ButtonMode ButtonType
        {
            get { return (ViewState["ButtonMode"] == null) ? ButtonMode.None : (ButtonMode)ViewState["ButtonMode"]; }
            set { ViewState["ButtonMode"] = value; }
        }

        public string BackgroundImageUrl
        {
            get { return (string)ViewState["BackgroundImageUrl"] ?? string.Empty; }
            set { ViewState["BackgroundImageUrl"] = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            if (this.Page != null)
            {
                string css = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "BugTracker.Buttons.Resources.btbuttons.css");

                HtmlLink cssLink = new HtmlLink();
                cssLink.Href = css;
                cssLink.ID = "btbuttons";
                cssLink.Attributes.Add("rel", "stylesheet");
                cssLink.Attributes.Add("type", "text/css");

                bool found = false;
                foreach (Control headctrl in Page.Header.Controls)
                {
                    if (headctrl.ID == cssLink.ID)
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    this.Page.Header.Controls.Add(cssLink);
                }

            }
            base.OnLoad(e);
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {

            base.AddAttributesToRender(writer);

            string imgname = string.Empty;

            switch (ButtonType)
            {
                case ButtonMode.None: imgname = ""; break;
                case ButtonMode.Add: imgname = "add"; break;
                case ButtonMode.Save: imgname = "save"; break;
                case ButtonMode.Cancel: imgname = "cancel"; break;
                case ButtonMode.ChangePassword: imgname = "pwd"; break;
                case ButtonMode.Refresh: imgname = "refresh"; break;
                case ButtonMode.Filter: imgname = "filter"; break;
                case ButtonMode.Default: imgname = "default"; break;
                case ButtonMode.Return: imgname = "return"; break;
                case ButtonMode.Delete: imgname = "del"; break;
                case ButtonMode.Preview: imgname = "preview"; break;
                case ButtonMode.Report: imgname = "report"; break;
                case ButtonMode.Excel: imgname = "excel"; break;
                case ButtonMode.Login: imgname = "login"; break;
            }

            string image = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), string.Format("BugTracker.Buttons.Resources.{0}.png", imgname));
            writer.AddAttribute("style", string.Format("background-image:url('{0}');", image));
            writer.AddAttribute("class", "bt-button");
        }
    }
}